<?php
/**
 * @title 200. 岛屿数量
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param String[][] $grid
     * @return Integer
     */
    function numIslands($grid) {
        /**
         * @since 2023-12-27 行数和列数
         */
        $rows = count($grid);
        $cols = count($grid[0]);

        /**
         * @since 2023-12-27 岛屿集合
         */
        $islandArray = [];

        /**
         * @since 2023-12-27 遍历数组
         */
        for($r=0; $r<$rows; $r++){
            for($c=0; $c<$cols; $c++){
                /**
                 * @since 2023-12-27 岛屿
                 */
                if($grid[$r][$c] == 1){
                    $islandArray["{$r}-{$c}"] = [$r, $c];
                } else {}
            }
        }

        /**
         * @since 2023-12-27 岛屿数量
         */
        $islandNum = 0;

        /**
         * @since 2023-12-27 计算岛屿的数量
         */
        while($islandArray){
            /**
             * @since 2023-12-27 pop一个岛屿
             */
            $island = array_pop($islandArray);

            /**
             * @since 2023-12-27 向周围延伸
             */
            $nodeArray = [$island];
            while($nodeArray){
                /**
                 * @since 2023-12-27 下次遍历的点集合
                 */
                $nextNodeArray = [];

                /**
                 * @since 2023-12-27 遍历当前岛屿集合
                 */
                foreach ($nodeArray as list($r, $c)){
                    /**
                     * @since 2023-12-27 4个方向移动数组
                     */
                    $moveArray = [
                        [-1,0], // 上
                        [1, 0], // 下
                        [0, -1], // 左
                        [0, 1], // 右
                    ];
                    foreach ($moveArray as list($addR, $addC)){
                        /**
                         * @since 2023-12-27 延伸点坐标
                         */
                        $newR = $r+$addR;
                        $newC = $c+$addC;

                        /**
                         * @since 2023-12-27 延伸点是否存在
                         */
                        $key = "{$newR}-{$newC}";
                        if(isset($islandArray[$key])){
                            $nextNodeArray[] = $islandArray[$key];

                            /**
                             * @since 2023-12-27 移除延伸点
                             */
                            unset($islandArray[$key]);
                        } else {}
                    }
                }

                /**
                 * @since 2023-12-27 下次遍历
                 */
                $nodeArray = $nextNodeArray;
            }

            /**
             * @since 2023-12-27 岛屿数量+1
             */
            $islandNum++;
        }

        /**
         * @return Int
         */
        return $islandNum;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.27_17.php
 */
$datas = [
    [[
        ["1","1","1","1","0"],
        ["1","1","0","1","0"],
        ["1","1","0","0","0"],
        ["0","0","0","0","0"]
    ]], // 1
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->numIslands(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
